package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-19
 */
@Api("添加小节信息")
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private VodClient vodClient;


    @Autowired
    private EduVideoService videoService;

    //添加小节
    @ApiOperation("添加小节")
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节 TODO 这个方法需要完善
    @ApiOperation("删除小节")
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable("id") String id){
        //远程调用vod的方法删除视频
        //根据小节id获取视频id，调用方法实现视频删除

            EduVideo eduVideo = videoService.getById(id);
            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                R reslut = vodClient.removeAlyVideo(videoSourceId);
                if(reslut.getCode() == 20001){
                    throw new GuliException(20001,"删除视频失败....熔断器");
                }
            }
            videoService.removeById(id);
            return R.ok();

    }
    //修改小节  TODO



}

